package com.hanhanxiaochong.zhongxiangchuanmei.view.order.act;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.OrderGoodDetailBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.OrderProjectDetailBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.WXPayBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.OrderDetailPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.act.ShopMapActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IOrderDetailView;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.ConfirmDialog;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.utils.TimeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/5/2.
 * Desc   : 订单待收货act
 */

public class OrderWaitTakeDeliveryActivity extends BaseActivity<OrderDetailPresenter> implements IOrderDetailView {


    @BindView(R.id.txt_left)
    TextView txtLeft;
    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.ll_leftview)
    RelativeLayout llLeftview;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.ll_rightview)
    RelativeLayout llRightview;
    @BindView(R.id.view_bottomline)
    View viewBottomline;
    @BindView(R.id.rl_topview)
    RelativeLayout rlTopview;
    @BindView(R.id.txt_countdown)
    TextView txtCountdown;
    @BindView(R.id.ll_container)
    LinearLayout llContainer;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_order_status)
    TextView tvOrderStatus;
    @BindView(R.id.tv_order_phone)
    TextView tvOrderPhone;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.tv_order_technician)
    TextView tvOrderTechnician;
    @BindView(R.id.tv_order_service_time)
    TextView tvOrderServiceTime;
    @BindView(R.id.tv_order_deliver_way)
    TextView tvOrderDeliverWay;
    @BindView(R.id.txt_check)
    TextView txtCheck;
    @BindView(R.id.txt_confirm)
    TextView txtConfirm;
    @BindView(R.id.ll_wait_takedelivery)
    LinearLayout llWaitTakedelivery;

    private String token;
    private String order_id;
    private String order_num;

    public static Intent createIntent(Context context, String order_id, String order_num) {
        Intent intent = new Intent(context, OrderWaitTakeDeliveryActivity.class);
        intent.putExtra("order_id", order_id);
        intent.putExtra("order_num", order_num);
        return intent;
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_order_wait_takedelivery);
        token = SharedPreferencesUtil.getsInstances(this).getLoginInfo().getToken();
        Intent intent = getIntent();
        order_id = intent.getStringExtra("order_id");
        order_num = intent.getStringExtra("order_num");
    }

    @Override
    protected void initPresenter() {
        mPresenter = new OrderDetailPresenter(this);

    }

    @Override
    protected void init() {
        setTopView();
        mPresenter.getGoodInfo(token, order_id, order_num);
    }

    private void setTopView() {
        imgLeft.setImageResource(R.mipmap.icon_left);
        txtTitle.setText("订单详情");
    }

    @OnClick({R.id.ll_leftview, R.id.txt_check, R.id.txt_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.txt_check:
                break;
            case R.id.txt_confirm:
                Bundle bundle = new Bundle();
                bundle.putString("content", "确认收货后款项将支付给商家?");
                ConfirmDialog dialog = ConfirmDialog.newInstance(bundle);
                dialog.setOnConfirmListener(() -> {
                    mPresenter.confirmDeliver(token, order_id);
                });
                dialog.show(getSupportFragmentManager(), "ConfirmDialog");
                break;
        }
    }

    @Override
    public void setGoodInfo(OrderGoodDetailBean data) {
        List<OrderGoodDetailBean.StoreBean> list = data.getStore();
        for (int i = 0; i < list.size(); i++) {
            OrderGoodDetailBean.StoreBean productBean = list.get(i);
            OrderGoodDetailBean.StoreBean.ListBean bean = productBean.getList();
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_detail, llContainer, false);
            TextView shopName = view.findViewById(R.id.tv_shop_name);
            shopName.setText(productBean.getS_name());
            view.findViewById(R.id.img_phone).setOnClickListener(v -> {
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + productBean.getPhone()));
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    ToastUtils.showToast("拨打电话权限被禁止");
                    return;
                }
                startActivity(intent);
            });
            view.findViewById(R.id.img_location).setOnClickListener(v -> startActivity(ShopMapActivity.createIntent(this, Double.parseDouble(productBean.getLat()),
                    Double.parseDouble(productBean.getLng()), productBean.getS_name(), "")));
            LinearLayout childContainer = view.findViewById(R.id.ll_container);
            View childView = LayoutInflater.from(mContext).inflate(R.layout.item_order_good_or_project, childContainer, false);
            ImageView ivGoodPic = childView.findViewById(R.id.iv_good_pic);
            ImageView ivSelfSupport = childView.findViewById(R.id.iv_self_support);
            TextView tvGoodName = childView.findViewById(R.id.tv_good_name);
            TextView tvVipPrice = childView.findViewById(R.id.tv_vip_price);
            TextView tvNormalPrice = childView.findViewById(R.id.tv_normal_price);

            GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + bean.getPic().split(",")[0], R.mipmap.img_error, R.mipmap.img_error, ivGoodPic);
            tvGoodName.setText(bean.getP_name());
            ivSelfSupport.setVisibility(bean.getSelf_support().equals("y") ? View.VISIBLE : View.GONE);
            tvVipPrice.setText(String.format(ResourcesUtils.getString(R.string.vip_good_price), bean.getPrice_vip()));
//                tvNormalPrice.setText(String.format(ResourcesUtils.getString(R.string.normal_good_price), bean.getPrice()));
            childContainer.addView(childView);
            if (i != 0 && productBean.getS_name().equals(list.get(i - 1).getS_name())) {
                view.findViewById(R.id.item_order_detail_header).setVisibility(View.GONE);
            }
            llContainer.addView(view);
        }

        tvOrderNum.setText(String.format(ResourcesUtils.getString(R.string.order_num), data.getOrder_num()));
        tvOrderPhone.setText(String.format(ResourcesUtils.getString(R.string.order_phone), SharedPreferencesUtil.getsInstances(this).getLoginInfo().getMobile()));
        tvOrderTime.setText(String.format(ResourcesUtils.getString(R.string.order_time), TimeUtils.getDateFromSeconds(data.getAdd_time())));
        tvOrderStatus.setText(String.format(ResourcesUtils.getString(R.string.order_status), "待收货"));
        tvOrderDeliverWay.setText(String.format(ResourcesUtils.getString(R.string.order_receive), data.getExpress()));
    }

    @Override
    public void setProjectInfo(OrderProjectDetailBean data) {

    }

    @Override
    public void hasDelivered(String data) {

    }

    @Override
    public void aliPay(String data) {

    }

    @Override
    public void wxPay(WXPayBean data) {

    }
}
